/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.editor.advanced;

import java.util.List;
import java.util.Set;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.DkBasePagingLoader;
import com.daikit.daikit4gxt.client.action.DkPagingToolbar;
import com.daikit.daikit4gxt.client.propertyaccess.DkEditDeletePropertyAccess;
import com.daikit.daikit4gxt.client.ui.editor.DkEditorFunctionality;
import com.daikit.daikit4gxt.client.utils.miscs.DkUIUtils;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;


/**
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <M1>
 * @param <M2>
 */
public abstract class DkDualAsyncPagingEditorGrid<M1 extends AbstractDkBeanWithId, M2 extends AbstractDkBeanWithId> extends DkDualEditorGrid<M1, M2>
{

	private final DkBasePagingLoader<M1, PagingLoadConfig, PagingLoadResult<M1>> basePagingLoader;

	/**
	 * Constructor
	 *
	 * @param editorRightStore
	 *           the {@link ListStore} that will be edited
	 * @param leftProperties
	 *           {@link PropertyAccess} for left grid
	 * @param rightProperties
	 *           {@link PropertyAccess} for right grid
	 * @param leftColumnConfigs
	 *           {@link ColumnConfig} list for left grid
	 * @param rightColumnConfigs
	 *           {@link ColumnConfig} list for right grid
	 * @param leftFunctionalities
	 *           a {@link Set} of {@link DkEditorFunctionality} for left {@link DkEditorGrid}
	 * @param rightFunctionalities
	 *           a {@link Set} of {@link DkEditorFunctionality} for right {@link DkEditorGrid}
	 * @param dualFunctionalities
	 *           a succession of DualFunctionality (don't fill this argument or give DualFunctionality.ALL to have all
	 *           functionalities)
	 */
	public DkDualAsyncPagingEditorGrid(final ListStore<M2> editorRightStore, final DkEditDeletePropertyAccess<M1> leftProperties,
			final DkEditDeletePropertyAccess<M2> rightProperties, final List<ColumnConfig<M1, ?>> leftColumnConfigs,
			final List<ColumnConfig<M2, ?>> rightColumnConfigs, final Set<DkEditorFunctionality> leftFunctionalities,
			final Set<DkEditorFunctionality> rightFunctionalities, final DualFunctionality... dualFunctionalities)
	{
		super(editorRightStore, leftProperties, rightProperties, leftColumnConfigs, rightColumnConfigs, leftFunctionalities, rightFunctionalities, false, true,
				dualFunctionalities);
		basePagingLoader = initializePagingLoader(getLeftStore());
		DkUIUtils.updateBottomToolbarStyle(basePagingLoader.getPagingToolbar());
		getLeftEditorGrid().getVerticalLayoutContainer().add(basePagingLoader.getPagingToolbar(), new VerticalLayoutData(1, -1));
	}

	private final DkBasePagingLoader<M1, PagingLoadConfig, PagingLoadResult<M1>> initializePagingLoader(final ListStore<M1> store)
	{
		final DkBasePagingLoader<M1, PagingLoadConfig, PagingLoadResult<M1>> basePagingLoader = new DkBasePagingLoader<M1, PagingLoadConfig, PagingLoadResult<M1>>(
				store, "Loading...", DkBaseMain.SCREEN_PAGING_SIZE, false)
		{
			@Override
			protected void doLoad(final PagingLoadConfig pagingLoadConfig, final DkBaseAsyncCallback<PagingLoadResult<M1>> callback)
			{
				callLoadLeftRpc(pagingLoadConfig, callback);
			}
		};
		return basePagingLoader;
	}

	protected abstract void callLoadLeftRpc(PagingLoadConfig pagingLoadConfig, AsyncCallback<PagingLoadResult<M1>> callback);

	@Override
	public void setEnabled(final boolean enabled)
	{
		super.setEnabled(enabled);
		basePagingLoader.getPagingToolbar().setEnabled(enabled);
	}

	/**
	 * @return the basePagingLoader
	 */
	public DkBasePagingLoader<M1, PagingLoadConfig, PagingLoadResult<M1>> getBasePagingLoader()
	{
		return basePagingLoader;
	}

	/**
	 * @return the pagingToolBar
	 */
	public DkPagingToolbar<M1, PagingLoadConfig, PagingLoadResult<M1>> getPagingToolBar()
	{
		return basePagingLoader.getPagingToolbar();
	}

	/**
	 * @return the action to reload the left panel
	 */
	public DkBaseAction<PagingLoadResult<M1>> getReloadLeftAction()
	{
		return basePagingLoader.getReloadAction();
	}

}
